#!/usr/bin/env python
#coding=utf-8

import time
from vic.exchange.vic_binance.binance_http import BinaHttp
from vic.exchange.vic_huobi.huobi_http import HuobiHttp
from vic.exchange.vic_okex.okex_http import OkexHttp
from vic.exchange.vic_okex.okexft_http import OkexftHttp

from lib.okex_risk_netvalue import OkexService
from lib.bina_risk_netvalue import BinanService
from lib.huobi_risk_netvalue import HuobiService

from lib.common import config_data, log_info, log_exception, timestamp2str, \
     current_strtime, before_after_timestamp, get_symbol, config_error_code_msg

conf = config_data()
BINA_URL       = conf.get('BINA_URL')
BINA_APIKEY    = conf.get('BINA_APIKEY')
BINA_APISECRET = conf.get('BINA_APISECRET')

HUOBI_URL       = conf.get('HUOBI_URL')
HUOBI_APIKEY    = conf.get('HUOBI_APIKEY')
HUOBI_APISECRET = conf.get('HUOBI_APISECRET')

OKEX_URL        = conf.get('OKEX_URL')
OKEX_APIKEY     = conf.get('OKEX_APIKEY')
OKEX_APISECRET  = conf.get('OKEX_APISECRET')

def accounts_reduce(currency='', multiple='', trading=''):
    """一键下单减仓"""

    bs = BinanService(BINA_URL, BINA_APIKEY, BINA_APISECRET)
    ok = OkexService(OKEX_URL, OKEX_APIKEY, OKEX_APISECRET)
    hs = HuobiService(HUOBI_URL, HUOBI_APIKEY, HUOBI_APISECRET)

    if trading == u'bina':
        bs.bina_cancel_allorder(currency)
        bs.account_order(0, currency, multiple)
    elif trading == u'huobi':
        hs.huobi_cancel_allorder(currency)
        hs.account_order(0, 0, 'sell-limit', currency, multiple)
    elif trading == u'okex':
        ok.okex_cancel_allorder(currency)
        ok.account_order(0, currency, multiple)
    elif trading == u'collect':
        bs.bina_cancel_allorder(currency)
        bs.account_order(0, currency, multiple)
        hs.huobi_cancel_allorder(currency)
        hs.account_order(0, 0, 'sell-limit', currency, multiple)
        ok.okex_cancel_allorder(currency)
        ok.account_order(0, currency, multiple)
    else:
        log_info(u'请选择要减仓的币种')
        return False


if __name__ == "__main__":
    accounts_reduce('trx', 0.5, 'bina')
